import { getVideBackListPage, getLiveBackSlide } from '@/server/news'
import { getNewsList } from '@/server/news'
import Page from '@/components/Layout'
import VideoBack from '@/components/VideoBack'
import { videoBackNavList } from '@/common/enum'
import { getLiveBackDetail, getShortVideoDetail, getSlideVideoBack } from '@/server/news'
import VideoTape from '@/components/VideoTape'
const Index = (props) => props?.isVideoTape ? <VideoTape {...props} /> : <VideoBack {...props} />;
Index.getLayout = (page, props) => {
  return (
    <Page header footer {...props}>
      {page}
    </Page >
  )
}
/**
 * 参数1 赛事拼音 
 * 参数2 带s的分页列表 
 * 
 * 参数1 赛事拼音
 * 参数2 不带s的是数字尝试直接播放
 * 
 * 参数1 是数字并且没有参数参数2 直接尝试播放 
 *
*/
export async function getServerSideProps (ctx) {
  // 判断是列表还是播放详情页面
  let props = {}
  const checkNumber = (value) => !isNaN(parseFloat(value)) && isFinite(value)
  let name = ctx?.params?.id?.[0] || videoBackNavList?.f?.[0]?.en //类别的拼音名称
  //播放的页面的条件  两个参数 并且 第二节参数的不是s开头 或者 第一个参数完全是数字
  if ((ctx?.params?.id?.length > 1 && !ctx?.params?.id?.[1]?.includes('s')) || (checkNumber(Number(name)) && ctx?.params?.id?.length === 1)) {
    //录像回放的页面
    let id = ctx?.params?.id?.[1]
    if (checkNumber(Number(name)) && ctx?.params?.id?.length === 1) id = Number(name)
    const type = '2'//这个地方只有录像
    let api = {
      "1": getShortVideoDetail,
      "2": getLiveBackDetail
    }
    const slideApi = {
      "1": getSlideVideoBack, //端视频
      "2": getLiveBackSlide,//回放
    }
    const res = await Reflect.get(api, type)(id)
    const slideParams = {
      "1": {
        num: 3,
        type: 4,
        typeId: res.typeId
      },
      "2": {
        num: 3,
        typeId: res.typeId,
        type: 1,
      }
    }


    const bottomApi = {
      "1": getSlideVideoBack, //短视频
      "2": getLiveBackSlide,//回放
    }
    const bottomParams = {
      "1": {
        num: 8,
        type: 4,
        typeId: res.typeId,
      },
      "2": {
        num: 8,
        typeId: res.typeId,
        type: 4,
      }
    }
    const [slideData, bottomData] = await Promise.all([Reflect.get(slideApi, type)(Reflect.get(slideParams, type)), Reflect.get(bottomApi, type)(Reflect.get(bottomParams, type))])
    props = {
      videoInfo: res,
      id,
      slideData,
      bottomData,
      isVideoTape: true,
      en: name,
      pageLink: "video",
    }
  } else {

    const currentInfo = Object.values(videoBackNavList).flat(1).find(r => r.en === name)
    const type = currentInfo?.typeGeneral
    let id = currentInfo?.id || ''
    if (currentInfo?.en === 'football' || currentInfo?.en === 'basketball') {
      id = currentInfo?.sid
    }
    const params = {
      pageNum: ctx?.params?.id?.[1]?.split("s")[1] || 1, //分页
      pageSize: 5,
      en: name,
      id: id//赛事id
    }
    const [videoList, everybody, videoData] = await Promise.all([getLiveBackSlide({ num: 6, type: 4, typeGeneral: type }), getNewsList(id, params.pageNum, params.pageSize), getVideBackListPage({ pageSize: params.pageSize, pageNum: params.pageNum, typeId: params.id }),])
    params.total = videoData.total
    params.current = videoData.current
    props = {
      params,
      videoData: videoData?.records,
      videoList,
      everybody: everybody?.records,
      everybodyId: id,
      type,
      videoType: 2,
      isVideoTape: false
    }
  }
  return {
    props: {
      ...props
    }
  }
}

export default Index